package br.ucb.musicbay.artista;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import br.ucb.musicbay.album.Album;
import br.ucb.musicbay.genero.Genero;
import br.ucb.musicbay.usuario.Usuario;

@Entity
@Table(name = "artista")
public class Artista implements Serializable {
	private static final long serialVersionUID = 9210163915685834246L;
	private Integer idArtista;
	private String nome;
	private String descricao;
	private byte[] img;
	private List<Genero> generos = new ArrayList<Genero>();
	private List<Album> albuns = new ArrayList<Album>();
	private List<Usuario> usuarios = new ArrayList<Usuario>();
	private Integer status;
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "usuario_has_artista", joinColumns = { @JoinColumn(name = "id_artista") }, inverseJoinColumns = { @JoinColumn(name = "id_usuario") })	
	public List<Usuario> getUsuarios() {
		return usuarios;
	}
	public void setUsuarios(List<Usuario> usuarios) {
		this.usuarios = usuarios;
	}
	
	@Lob
	@Column(name = "img")
	public byte[] getImg() {
		return img;
	}
	public void setImg(byte[] img) {		
		this.img = img;
	}
	
	@OneToMany(mappedBy = "artista")
	public List<Album> getAlbuns() {
		return albuns;
	}
	public void setAlbuns(List<Album> albuns) {
		this.albuns = albuns;
	}

	@Id
	@GeneratedValue
	@Column(name = "id_artista")
	public Integer getIdArtista() {
		return idArtista;
	}

	public void setIdArtista(Integer idArtista) {
		this.idArtista = idArtista;
	}

	@Column(name = "status")
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}	
	
	@Column(name = "nome")
	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	@Column(name = "descricao")
	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	
	@ManyToMany(cascade = CascadeType.PERSIST)
	@JoinTable(name = "genero_has_artista", joinColumns = { @JoinColumn(name = "id_artista") }, inverseJoinColumns = { @JoinColumn(name = "id_genero") })
	public List<Genero> getGeneros() {
		return generos;
	}

	public void setGeneros(List<Genero> generos) {
		this.generos = generos;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((albuns == null) ? 0 : albuns.hashCode());
		result = prime * result + ((descricao == null) ? 0 : descricao.hashCode());
		result = prime * result + ((generos == null) ? 0 : generos.hashCode());
		result = prime * result + ((idArtista == null) ? 0 : idArtista.hashCode());
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result + ((usuarios == null) ? 0 : usuarios.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Artista other = (Artista) obj;
		if (albuns == null) {
			if (other.albuns != null)
				return false;
		} else if (!albuns.equals(other.albuns))
			return false;
		if (descricao == null) {
			if (other.descricao != null)
				return false;
		} else if (!descricao.equals(other.descricao))
			return false;
		if (generos == null) {
			if (other.generos != null)
				return false;
		} else if (!generos.equals(other.generos))
			return false;
		if (idArtista == null) {
			if (other.idArtista != null)
				return false;
		} else if (!idArtista.equals(other.idArtista))
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		if (usuarios == null) {
			if (other.usuarios != null)
				return false;
		} else if (!usuarios.equals(other.usuarios))
			return false;
		return true;
	}
	
	
}
